local ScoreboardSummary = CreateFrame("Frame", "ScoreboardSummary", UIParent)
local L = ScoreboardSummary.Locale

L = {
	["\n|cffffffff Killing Blows: |cffffd200"] = "\n|cffffffff Killing Blows: |cffffd200",
	["\n|cffffffff Honor Kills: |cffffd200"] = "\n|cffffffff Honor Kills: |cffffd200",
	["\n|cffffffff Deaths: |cffffd200"] = "\n|cffffffff Deaths: |cffffd200",
	["\n|cffffffff Bonus Honor: |cffffd200"] = "\n|cffffffff Bonus Honor: |cffffd200",
	["\n|cffffffff Damage Done: |cffffd200"] = "\n|cffffffff Damage Done: |cffffd200",
	["\n|cffffffff Healing Done: |cffffd200"] = "\n|cffffffff Healing Done: |cffffd200",
	["|cffff2020Enemies|cffffffff - |cff20ff20Friends"] = "|cffff2020Enemies|cffffffff - |cff20ff20Friends",
	["\n|cffffffff Priests: |cffff2020"] = "\n|cffffffff Priests: |cffff2020",
	["\n|cffff7f00 Druids: |cffff2020"] = "\n|cffff7f00 Druids: |cffff2020",
	["\n|cffff3399 Paladans: |cffff2020"] = "\n|cffff3399 Paladans: |cffff2020",
	["\n|cff33cccc Shamans: |cffff2020"] = "\n|cff33cccc Shamans: |cffff2020",
	["\n|cffc7c7cf Total Healers: |cffff2020"] = "\n|cffc7c7cf Total Healers: |cffff2020",
	["|cffeda55f Warriors: |cffff2020"] = "|cffeda55f Warriors: |cffff2020",
	["\n|cff9980CC Warlocks: |cffff2020"] = "\n|cff9980CC Warlocks: |cffff2020",
	["\n|cff00ffff Mages: |cffff2020"] = "\n|cff00ffff Mages: |cffff2020",
	["\n|cff66cc99 Hunters: |cffff2020"] = "\n|cff66cc99 Hunters: |cffff2020",
	["\n|cffffff00 Rogues: |cffff2020"] = "\n|cffffff00 Rogues: |cffff2020",
	["\n|cff9933ff Death Knights: |cffff2020"] = "\n|cff9933ff Death Knights: |cffff2020",
	["\n|cffc7c7cf Total DPS: |cffff2020"] = "\n|cffc7c7cf Total DPS: |cffff2020",
	["\n|cffc7c7cf Servers:"] = "\n|cffc7c7cf Servers:",
	["Priest"] = "Priest",
	["Druid"] = "Druid",
	["Warrior"] = "Warrior",
	["Paladin"] = "Paladin",
	["Shaman"] = "Shaman",
	["Warlock"] = "Warlock",
	["Mage"] = "Mage",
	["Hunter"] ="Hunter",
	["Rogue"] = "Rogue",
	["Death Knight"] = "Death Knight",
}
if GetLocale() == "zhCN" then
	L = {
		["\n|cffffffff Killing Blows: |cffffd200"] = "\n|cffffffff 击杀: |cffffd200",
		["\n|cffffffff Honor Kills: |cffffd200"] = "\n|cffffffff 荣誉击杀: |cffffd200",
		["\n|cffffffff Deaths: |cffffd200"] = "\n|cffffffff 死亡: |cffffd200",
		["\n|cffffffff Bonus Honor: |cffffd200"] = "\n|cffffffff 荣誉奖励: |cffffd200",
		["\n|cffffffff Damage Done: |cffffd200"] = "\n|cffffffff 伤害量: |cffffd200",
		["\n|cffffffff Healing Done: |cffffd200"] = "\n|cffffffff 治疗量: |cffffd200",
		["|cffff2020Enemies|cffffffff - |cff20ff20Friends"] = "|cffff2020敌对|cffffffff - |cff20ff20友方",
		["\n|cffffffff Priests: |cffff2020"] = "\n|cffffffff 牧师: |cffff2020",
		["\n|cffff7f00 Druids: |cffff2020"] = "\n|cffff7f00 德鲁伊: |cffff2020",
		["\n|cffff3399 Paladans: |cffff2020"] = "\n|cffff3399 圣骑士: |cffff2020",
		["\n|cff33cccc Shamans: |cffff2020"] = "\n|cff33cccc 萨满祭司: |cffff2020",
		["\n|cffc7c7cf Total Healers: |cffff2020"] = "\n|cffc7c7cf 总治疗: |cffff2020",
		["|cffeda55f Warriors: |cffff2020"] = "|cffeda55f 战士: |cffff2020",
		["\n|cff9980CC Warlocks: |cffff2020"] = "\n|cff9980CC 术士: |cffff2020",
		["\n|cff00ffff Mages: |cffff2020"] = "\n|cff00ffff 法师: |cffff2020",
		["\n|cff66cc99 Hunters: |cffff2020"] = "\n|cff66cc99 猎人: |cffff2020",
		["\n|cffffff00 Rogues: |cffff2020"] = "\n|cffffff00 潜行者: |cffff2020",
		["\n|cff9933ff Death Knights: |cffff2020"] = "\n|cff9933ff 死亡骑士: |cffff2020",
		["\n|cffc7c7cf Total DPS: |cffff2020"] = "\n|cffc7c7cf 总DPS: |cffff2020",
		["\n|cffc7c7cf Servers:"] = "\n|cffc7c7cf 服务器:",
		["Priest"] = "牧师",
		["Druid"] = "德鲁伊",
		["Warrior"] = "战士",
		["Paladin"] = "圣骑士",
		["Shaman"] = "萨满祭司",
		["Warlock"] = "术士",
		["Mage"] = "法师",
		["Hunter"] ="猎人",
		["Rogue"] = "潜行者",
		["Death Knight"] = "死亡骑士",
	}
end
if GetLocale() == "zhTW" then
	L = {
		["\n|cffffffff Killing Blows: |cffffd200"] = "\n|cffffffff 擊殺: |cffffd200",
		["\n|cffffffff Honor Kills: |cffffd200"] = "\n|cffffffff 榮譽擊殺: |cffffd200",
		["\n|cffffffff Deaths: |cffffd200"] = "\n|cffffffff 死亡: |cffffd200",
		["\n|cffffffff Bonus Honor: |cffffd200"] = "\n|cffffffff 榮譽獎勵: |cffffd200",
		["\n|cffffffff Damage Done: |cffffd200"] = "\n|cffffffff 傷害量: |cffffd200",
		["\n|cffffffff Healing Done: |cffffd200"] = "\n|cffffffff 治療量: |cffffd200",
		["|cffff2020Enemies|cffffffff - |cff20ff20Friends"] = "|cffff2020敵對|cffffffff - |cff20ff20友方",
		["\n|cffffffff Priests: |cffff2020"] = "\n|cffffffff 牧師: |cffff2020",
		["\n|cffff7f00 Druids: |cffff2020"] = "\n|cffff7f00 德魯伊: |cffff2020",
		["\n|cffff3399 Paladans: |cffff2020"] = "\n|cffff3399 聖騎士: |cffff2020",
		["\n|cff33cccc Shamans: |cffff2020"] = "\n|cff33cccc 薩滿: |cffff2020",
		["\n|cffc7c7cf Total Healers: |cffff2020"] = "\n|cffc7c7cf 總治療: |cffff2020",
		["|cffeda55f Warriors: |cffff2020"] = "|cffeda55f 戰士: |cffff2020",
		["\n|cff9980CC Warlocks: |cffff2020"] = "\n|cff9980CC 術士: |cffff2020",
		["\n|cff00ffff Mages: |cffff2020"] = "\n|cff00ffff 法師: |cffff2020",
		["\n|cff66cc99 Hunters: |cffff2020"] = "\n|cff66cc99 獵人: |cffff2020",
		["\n|cffffff00 Rogues: |cffff2020"] = "\n|cffffff00 盜賊: |cffff2020",
		["\n|cff9933ff Death Knights: |cffff2020"] = "\n|cff9933ff 死亡騎士: |cffff2020",
		["\n|cffc7c7cf Total DPS: |cffff2020"] = "\n|cffc7c7cf 總DPS: |cffff2020",
		["\n|cffc7c7cf Servers:"] = "\n|cffc7c7cf 伺服器:",
		["Priest"] = "牧師",
		["Druid"] = "德魯伊",
		["Warrior"] = "戰士",
		["Paladin"] = "圣騎士",
		["Shaman"] = "薩滿",
		["Warlock"] = "術士",
		["Mage"] = "法師",
		["Hunter"] ="獵人",
		["Rogue"] = "盜賊",
		["Death Knight"] = "死亡騎士",
	}
end

SSumFrame = CreateFrame("Frame", "SSumFrame", UIParent)
SSumFrame:SetWidth(650) SSumFrame:SetHeight(150)
SSumFrame:SetMovable(true); SSumFrame:EnableMouse(true)
SSumFrame:SetParent("WorldStateScoreFrame")

SSumFrame:SetBackdrop({bgFile = "Interface/DialogFrame/UI-DialogBox-Background", 
					edgeFile = "Interface/DialogFrame/UI-DialogBox-Border",
					insets = { left = 5, right = 6, top = 6, bottom = 5 }}
)

-- HEADER
SSumFrame.header = CreateFrame("Frame", "SSumFrame", UIParent)
SSumFrame.header:SetFrameStrata("BACKGROUND")
SSumFrame.header:SetParent("SSumFrame")
SSumFrame.header:SetWidth(650) SSumFrame.header:SetHeight(33)
SSumFrame.header:SetPoint("CENTER", SSumFrame, "TOP", 0, 0)
SSumFrame.header:SetBackdrop({bgFile = "Interface/DialogFrame/UI-DialogBox-Background", 
												edgeFile = "Interface/DialogFrame/UI-DialogBox-Border", 
												tile = true, tileSize = 25, edgeSize = 25, 
												insets = { left = 5, right = 6, top = 6, bottom = 5 }});
SSumFrame.headertext = SSumFrame.header:CreateFontString(nil, "BACKGROUND")
SSumFrame.headertext:SetFontObject(GameFontNormalSmall)
SSumFrame.headertext:SetWidth(650) SSumFrame.headertext:SetHeight(25)
SSumFrame.headertext:SetPoint("CENTER", SSumFrame.header, "CENTER", 0, 0)
SSumFrame.headertext:SetJustifyH("MIDDLE") SSumFrame.headertext:SetJustifyV("MIDDLE")
SSumFrame.headertext:SetText("|cffffffffScoreboard Summary")

-- HOOK EVENTS
SSumFrame:SetScript("OnMouseDown", function() SSumFrame:StartMoving() end)
SSumFrame:SetScript("OnMouseUp", function() SSumFrame:StopMovingOrSizing() end)
SSumFrame:SetScript("OnDragStop", function() SSumFrame:StopMovingOrSizing() end)
SSumFrame:SetScript("OnEnter", function() GameTooltip:SetOwner(this, "ANCHOR_CURSOR"); GameTooltip:SetText("Left-click to drag.") end)
SSumFrame:SetScript("OnLeave", function() GameTooltip:Hide() end)

-- TEXT AREAS
SSumText1 = SSumFrame:CreateFontString(nil, "BACKGROUND")
SSumText1:SetFontObject(GameFontNormalSmall)
SSumText1:SetWidth(200) SSumText1:SetHeight(150)
SSumText1:SetPoint("TOPLEFT", SSumFrame, "TOPLEFT", 15, -15)
SSumText1:SetJustifyH("LEFT") SSumText1:SetJustifyV("TOP")
SSumText1:SetParent("SSumFrame")

SSumText10 = SSumFrame:CreateFontString(nil, "BACKGROUND")
SSumText10:SetFontObject(GameFontNormalSmall)
SSumText10:SetWidth(200) SSumText10:SetHeight(150)
SSumText10:SetPoint("TOPLEFT", SSumFrame, "TOPLEFT", 0, -15)
SSumText10:SetJustifyH("RIGHT") SSumText10:SetJustifyV("TOP")
SSumText10:SetParent("SSumFrame")

SSumText2 = SSumFrame:CreateFontString(nil, "BACKGROUND")
SSumText2:SetFontObject(GameFontNormalSmall)
SSumText2:SetWidth(200) SSumText2:SetHeight(150)
SSumText2:SetPoint("TOPLEFT", SSumFrame, "TOPLEFT", 215, -15)
SSumText2:SetJustifyH("LEFT") SSumText2:SetJustifyV("TOP")
SSumText2:SetParent("SSumFrame")

SSumText20 = SSumFrame:CreateFontString(nil, "BACKGROUND")
SSumText20:SetFontObject(GameFontNormalSmall)
SSumText20:SetWidth(200) SSumText20:SetHeight(150)
SSumText20:SetPoint("TOPLEFT", SSumFrame, "TOPLEFT", 215, -15)
SSumText20:SetJustifyH("RIGHT") SSumText20:SetJustifyV("TOP")
SSumText20:SetParent("SSumFrame")

SSumText3 = SSumFrame:CreateFontString(nil, "BACKGROUND")
SSumText3:SetFontObject(GameFontNormalSmall)
SSumText3:SetWidth(200) SSumText3:SetHeight(150)
SSumText3:SetPoint("TOPLEFT", SSumFrame, "TOPLEFT", 430, -15)
SSumText3:SetJustifyH("LEFT") SSumText3:SetJustifyV("TOP")
SSumText3:SetParent("SSumFrame")

SSumText30 = SSumFrame:CreateFontString(nil, "BACKGROUND")
SSumText30:SetFontObject(GameFontNormalSmall)
SSumText30:SetWidth(200) SSumText30:SetHeight(150)
SSumText30:SetPoint("TOPLEFT", SSumFrame, "TOPLEFT", 430, -15)
SSumText30:SetJustifyH("RIGHT") SSumText30:SetJustifyV("TOP")
SSumText30:SetParent("SSumFrame")
	
SSumFrame:SetPoint("CENTER", UIParent, "CENTER", 0, 330)

-- SCOREBOARD SUMMARY ONUPDATE
ScoreboardSummary:RegisterEvent("ADDON_LOADED")
ScoreboardSummary:SetScript("OnEvent", function(self, event)
	if event == "ADDON_LOADED" then
		MinimalisqueDB = MinimalisqueDB or {}
		if MinimalisqueDB.ScoreboardSummary == nil then MinimalisqueDB.ScoreboardSummary = true end
	end
end)
ScoreboardSummary:SetScript("OnUpdate", function(self)
    if MinimalisqueDB['ScoreboardSummary'] == false and SSumFrame:IsVisible() then
        SSumFrame:Hide()
    elseif (WorldStateScoreFrame:IsVisible() and MinimalisqueDB['ScoreboardSummary'] == true) then
        SSumFrame:Show()
        local servers = {}
        local yourservers = {}
        local name, kb, hk, deaths, bonus, faction, rank, race, class, damage, healing, server2 =
                "nil", 0, 0, 0, 0, "nil", "nil", "nil", "nil", 0, 0, "nil"
        local priests, druids, warriors, paladins, warlocks, mages, hunters, rogues, dks, shamans, healers, dps, count =
                0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
        local yourpriests, yourdruids, yourwarriors, yourpaladins, yourwarlocks, yourmages, yourhunters, yourrogues, yourdks, yourshamans, yourhealers, yourdps, yourcount =
                0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
        local PlayerName, PlayerKillingBlows, PlayerHonorKills, PlayerDeaths, PlayerHonorGained, PlayerFaction, PlayerRank, PlayerRace, PlayerClass, PlayerFilename, PlayerDamageDone, PlayerHealingDone =
                "nil", 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
        
        for i = 1, GetNumBattlefieldScores() do
            name, kb, hk, deaths, bonus, faction, rank, race, class, damage, healing = GetBattlefieldScore(i)
            
	if (UnitFactionGroup("player") == "Horde") then
                if (faction == 1) then
                    if (class == L["Priest"]) then priests = priests + 1; healers = healers + 1 end
                    if (class == L["Druid"]) then druids = druids + 1; healers = healers + 1 end
                    if (class == L["Warrior"]) then warriors = warriors + 1; dps = dps + 1 end
                    if (class == L["Paladin"]) then paladins = paladins + 1; healers = healers + 1 end
                    if (class == L["Shaman"]) then shamans = shamans + 1; healers = healers + 1 end
                    if (class == L["Warlock"]) then warlocks = warlocks + 1; dps = dps + 1 end
                    if (class == L["Mage"]) then mages = mages + 1; dps = dps + 1 end
                    if (class == L["Hunter"]) then hunters = hunters + 1; dps = dps + 1 end
                    if (class == L["Rogue"]) then rogues = rogues + 1; dps = dps + 1 end 
					if (class == L["Death Knight"]) then dks = dks + 1; dps = dps + 1 end 
                    if (name) then if (not string.find(name, "-")) then server2 = GetRealmName()
                    else _, server2 = strsplit("-", name, 2) end
                    if (servers[server2] == nil) then servers[server2] = 1; count = count + 1 end end
                end
                if (faction == 0) then
                    if (class == L["Priest"]) then yourpriests = yourpriests + 1; yourhealers = yourhealers + 1 end
                    if (class == L["Druid"]) then yourdruids = yourdruids + 1; yourhealers = yourhealers + 1 end
                    if (class == L["Warrior"]) then yourwarriors = yourwarriors + 1; yourdps = yourdps + 1 end
                    if (class == L["Paladin"]) then yourpaladins = yourpaladins + 1; yourhealers = yourhealers + 1 end
                    if (class == L["Shaman"]) then yourshamans = yourshamans + 1; yourhealers = yourhealers + 1 end
                    if (class == L["Warlock"]) then yourwarlocks = yourwarlocks + 1; yourdps = yourdps + 1 end
                    if (class == L["Mage"]) then yourmages = yourmages + 1; yourdps = yourdps + 1 end
                    if (class == L["Hunter"]) then yourhunters = yourhunters + 1; yourdps = yourdps + 1 end
                    if (class == L["Rogue"]) then yourrogues = yourrogues + 1; yourdps = yourdps + 1 end
					if (class == L["Death Knight"]) then yourdks = yourdks + 1; yourdps = yourdps + 1 end
                    if (name) then if (not string.find(name, "-")) then server2 = GetRealmName()
                    else _, server2 = strsplit("-", name, 2) end
                    if (yourservers[server2] == nil) then yourservers[server2] = 1; yourcount = yourcount + 1 end end
                end
            end
            if (UnitFactionGroup("player") == "Alliance") then
                if (faction == 0) then
                    if (class == L["Priest"]) then priests = priests + 1; healers = healers + 1 end
                    if (class == L["Druid"]) then druids = druids + 1; healers = healers + 1 end
                    if (class == L["Warrior"]) then warriors = warriors + 1; dps = dps + 1 end
                    if (class == L["Shaman"]) then shamans = shamans + 1; healers = healers + 1 end
                    if (class == L["Paladin"]) then paladins = paladins + 1; healers = healers + 1 end
                    if (class == L["Warlock"]) then warlocks = warlocks + 1; dps = dps + 1 end
                    if (class == L["Mage"]) then mages = mages + 1; dps = dps + 1 end
                    if (class == L["Hunter"]) then hunters = hunters + 1; dps = dps + 1 end
                    if (class == L["Rogue"]) then rogues = rogues + 1; dps = dps + 1 end
					if (class == L["Death Knight"]) then dks = dks + 1; dps = dps + 1 end 
                    if (name) then if (not string.find(name, "-")) then server2 = GetRealmName()
                    else _, server2 = strsplit("-", name, 2) end
                    if (servers[server2] == nil) then servers[server2] = 1; count = count + 1 end end
                end
                if (faction == 1) then
                    if (class == L["Priest"]) then yourpriests = yourpriests + 1; yourhealers = yourhealers + 1 end
                    if (class == L["Druid"]) then yourdruids = yourdruids + 1; yourhealers = yourhealers + 1 end
                    if (class == L["Warrior"]) then yourwarriors = yourwarriors + 1; yourdps = yourdps + 1 end
                    if (class == L["Paladin"]) then yourpaladins = yourpaladins + 1; yourhealers = yourhealers + 1 end
                    if (class == L["Shaman"]) then yourshamans = yourshamans + 1; yourhealers = yourhealers + 1 end
                    if (class == L["Warlock"]) then yourwarlocks = yourwarlocks + 1; yourdps = yourdps + 1 end
                    if (class == L["Mage"]) then yourmages = yourmages + 1; yourdps = yourdps + 1 end
                    if (class == L["Hunter"]) then yourhunters = yourhunters + 1; yourdps = yourdps + 1 end
                    if (class == L["Rogue"]) then yourrogues = yourrogues + 1; yourdps = yourdps + 1 end 
					if (class == L["Death Knight"]) then yourdks = yourdks + 1; yourdps = yourdps + 1 end
                    if (name) then if (not string.find(name, "-")) then server2 = GetRealmName()
                    else _, server2 = strsplit("-", name, 2) end
                    if (yourservers[server2] == nil) then yourservers[server2] = 1; yourcount = yourcount + 1 end end
                end
            end
         
            if name then if string.find(name, UnitName("player")) then
                PlayerName, PlayerKillingBlows, PlayerHonorKills, PlayerDeaths, PlayerHonorGained, PlayerFaction, PlayerRank, PlayerRace, PlayerClass, PlayerFilename, PlayerDamageDone, PlayerHealingDone = GetBattlefieldScore(i)
            end end
        end
        -- write data
        SSumText1:SetText("|cffffffff"..PlayerName..
                                        L["\n|cffffffff Killing Blows: |cffffd200"].. 
                                        L["\n|cffffffff Honor Kills: |cffffd200"].. 
                                        L["\n|cffffffff Deaths: |cffffd200"].. 
                                        L["\n|cffffffff Bonus Honor: |cffffd200"].. 
                                        L["\n|cffffffff Damage Done: |cffffd200"].. 
                                        L["\n|cffffffff Healing Done: |cffffd200"])
        SSumText2:SetText(L["|cffff2020Enemies|cffffffff - |cff20ff20Friends"]..
                                        L["\n|cffffffff Priests: |cffff2020"].. 
                                        L["\n|cffff7f00 Druids: |cffff2020"].. 
                                        L["\n|cffff3399 Paladans: |cffff2020"].. 
                                        L["\n|cff33cccc Shamans: |cffff2020"].. 
                                        L["\n|cffc7c7cf Total Healers: |cffff2020"])
        SSumText3:SetText(L["|cffeda55f Warriors: |cffff2020"].. 
                                        L["\n|cff9980CC Warlocks: |cffff2020"].. 
                                        L["\n|cff00ffff Mages: |cffff2020"].. 
                                        L["\n|cff66cc99 Hunters: |cffff2020"].. 
                                        L["\n|cffffff00 Rogues: |cffff2020"]..
										L["\n|cff9933ff Death Knights: |cffff2020"]..
                                        L["\n|cffc7c7cf Total DPS: |cffff2020"].. 
                                        L["\n|cffc7c7cf Servers:"])
        SSumText10:SetText("\n".. PlayerKillingBlows ..
                                        "\n".. PlayerHonorKills ..
                                        "\n".. PlayerDeaths ..
                                        "\n".. PlayerHonorGained ..
                                        "\n".. PlayerDamageDone ..
                                        "\n".. PlayerHealingDone ..
                                        "\n \n")
        SSumText20:SetText("\n|cffff2020".. priests .."|cffffffff - |cff20ff20".. yourpriests ..
                                        "\n|cffff2020".. druids .."|cffffffff - |cff20ff20".. yourdruids ..
                                        "\n|cffff2020".. paladins .."|cffffffff - |cff20ff20".. yourpaladins ..
                                        "\n|cffff2020".. shamans .."|cffffffff - |cff20ff20".. yourshamans ..
                                        "\n|cffff2020".. healers .."|cffffffff - |cff20ff20".. yourhealers)
        SSumText30:SetText("|cffff2020".. warriors .."|cffffffff - |cff20ff20".. yourwarriors ..
                                        "\n|cffff2020".. warlocks .."|cffffffff - |cff20ff20".. yourwarlocks ..
                                        "\n|cffff2020".. mages .."|cffffffff - |cff20ff20".. yourmages ..
                                        "\n|cffff2020".. hunters .."|cffffffff - |cff20ff20".. yourhunters ..
                                        "\n|cffff2020".. rogues .."|cffffffff - |cff20ff20".. yourrogues ..
										"\n|cffff2020".. dks .."|cffffffff - |cff20ff20".. yourdks ..
                                        "\n|cffff2020".. dps .."|cffffffff - |cff20ff20".. yourdps ..
                                        "\n|cffff2020".. count .."|cffffffff - |cff20ff20".. yourcount)
    end
end)